import { createSlice, PayloadAction } from "@reduxjs/toolkit";

//React-Redux官网下toolkit使用实例

export interface LoginState {
  username: string;
  token: string;
  loginStatus: boolean;
}

const initialState: LoginState = {
  username: "",
  token: "",
  loginStatus: false,
};

export const LoginStore = createSlice({
  name: "login",
  initialState,
  reducers: {
    login: (state, action: PayloadAction<LoginState>) => {
      state.username = action.payload.username;
      //state.password = decrypt(action.payload.password);
      state.token = action.payload.token;
      state.loginStatus = action.payload.loginStatus;
    },
    logout: (state) => {
      state.username = "";
      state.token = "";
      state.loginStatus = false;
    },
  },
  selectors: {
    selectUsername: (state: LoginState) => state.username,
    //selectPassword: (state:LoginState) => state.password,
    selectToken: (state: LoginState) => state.token,
    selectLoginStatus: (state: LoginState) => state.loginStatus,
  },
});

export const { login, logout } = LoginStore.actions;
export const { selectUsername, selectToken, selectLoginStatus } = LoginStore.selectors;
